package com.jfl.core.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

import com.jfl.listener.ApplicationReadyListener;
import com.jfl.system.SysCacheService;
import com.jfl.system.SysSessionService;
import com.jfl.system.SysUserService;

/**
 * 初始化用户权限以及缓存
 * 
 * @author 赵炎
 * @version [V1.00, 2018年11月30日]
 * @since V1.00
 */
@Component
public class SysServerListener extends ApplicationReadyListener
{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
    public void onApplicationEvent(ApplicationEvent event)
    {
        if (event instanceof ApplicationReadyEvent)
        {
            // 应用已启动完成
            ConfigurableApplicationContext context = ((ApplicationReadyEvent)event).getApplicationContext();
            // 刷新缓存
            context.getBean(SysCacheService.class).flush();
            // 初始化用户信息
            context.getBean(SysUserService.class).init();
            // 删除过期缓存
            context.getBean(SysSessionService.class).deleteExpireSession();
        }
        else if (event instanceof ContextStoppedEvent)
        {
            // 应用停止
            //ProtocolConfig
        }
        else if (event instanceof ContextClosedEvent)
        {
            // 应用关闭
            //ProtocolConfig.destroyAll();
            //Proto
        }
        super.onApplicationEvent(event);
    }
    
}
